package top.carrotguo.weibo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.List;

import top.carrotguo.weibo.R;
import top.carrotguo.weibo.utils.EmojiUtils;

public class EmojiAdapter extends BaseAdapter {

    List<String> datas;
    Context mContext;
    private int mPadding;
    private int mWidth;
    private EmojiListener mListener;

    public EmojiAdapter(Context context,List<String> datas,int padding,int width){
        this.datas = datas;
        this.mContext = context;
        mPadding = padding;
        mWidth = width;
    }

    @Override
    public int getCount() {
        //增加一个回退按钮
        return datas.size()+1;
    }

    @Override
    public String getItem(int position) {
        if (position < datas.size()) {
            return datas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        GridView.LayoutParams params = new AbsListView.LayoutParams(mWidth,mWidth);
        imageView.setPadding(mWidth/8,mWidth/8,mWidth/8,mWidth/8);
        imageView.setLayoutParams(params);
        imageView.setBackgroundResource(R.drawable.selector_emoji_bg);
        if (position == getCount()-1) {
            //回退
            imageView.setImageResource(R.drawable.selector_delete);
            if (mListener != null) {
                mListener.onDelete();
            }
        } else {
            //表情
            //加载表情
            imageView.setImageResource(EmojiUtils.getImgByName(getItem(position)));
            if (mListener != null) {
                mListener.onSelect(position);
            }
        }

        return imageView;
    }

    public void setEmojiListener(EmojiListener listener){
        mListener = listener;
    }

    public interface EmojiListener{
        void onDelete();
        void onSelect(int position);
    }

}
